﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.AspNetCore.Http;
using Microsoft.EntityFrameworkCore;
using deviceManage.Models;
using deviceManage.vo;
using deviceManage.Tools;
using Microsoft.AspNetCore.Mvc.Filters;

namespace deviceManage.Controllers
{
    /// <summary>
    /// 登录控制器
    /// </summary>
    public class ModifyPasswordController : Controller
    {
        private readonly DeviceCtx _context;

        public ModifyPasswordController(DeviceCtx context)
        {
            _context = context;
        }

        //列表主页
        public IActionResult Index()
        {
            return View();
        }

        /// <summary>
        ///修改密码
        /// </summary>
        /// <param name="name"></param>
        /// <param name="password"></param>
        /// <param name="rememberPass"></param>
        /// <returns></returns>
        public AjaxResult DoPassModify(string oldPassword, string password)
        {
            
            string keyId = HttpContext.Session.GetString(WebContext.SESSION_KEY_USER_ID);
            User user = this._context.User.Single(p => p.Id == keyId);
            if (!oldPassword.Equals(user.Pass))
            {
                return AjaxResult.GetError("原密码错误，请重新输入！");
            }
            else {
                user.Pass = password;
                _context.Update(user);
                _context.SaveChanges();
                return AjaxResult.GetOK("修改成功！");
            }
 
        }


    }
}
